$(function () {
    init_userAddForm();
    init_grade();
});

/**初始化信息**/
function init_userAddForm() {
    var $userAddForm = $("#user-add-form");
    //验证规则初始化
    var icon = "<i class='zmdi zmdi-close-circle zmdi-hc-fw color-red'></i> ";
    var validator = $userAddForm.validate({
        rules: {
            username: {
                required: true,
                minlength: 3,
                maxlength: 20,
                remote: {
                    url: ctx+"user/checkUserName",
                    type: "get",
                    dataType: "json",
                    data: {
                        username: function (){
                            return $("#username").val().trim();
                        }
                    }
                }
            },
            nickname:{required: true},
            roles: {required: true}
        },
        messages: {
            username: {
                required: icon + "请输入用户名",
                minlength: icon + "用户名长度3到10个字符",
                remote: icon + "用户名已经存在"
            },
            nickname:icon + "请填写用户昵称",
            roles: icon + "请选择用户角色"
        }
    });
    /**初始化角色**/
    var $rolesSelect = $userAddForm.find("select[name='rolesSelect']");
    var $roles = $userAddForm.find("input[name='roles']");
    $.post(ctx + "role/list", {}, function (r) {
        let data = r.rows;
        let option = "";
        for (let i = 0; i < data.length; i++) {
            option += "<option value='" + data[i].roleId + "'>" + data[i].roleName + "</option>"
        }
        $rolesSelect.html("").append(option);
        let options = {
            selectAllText: '所有角色',
            allSelected: '所有角色',
            width: '100%',
            onClose: function () {
                $roles.val($rolesSelect.val());
                validator.element("input[name='roles']");
            }
        };
        $rolesSelect.multipleSelect(options);
    });
    /**初始化部门树**/
    $.post(ctx + "dept/tree", {}, function (r) {
        if (r.code === 0) {
            var data = r.msg;
            $('#deptTree').jstree({
                "core": {
                    'data': data.children,
                    'multiple': false // 取消多选
                },
                "state": {
                    "disabled": true
                },
                "checkbox": {
                    "three_state": false // 取消选择父节点后选中所有子节点
                },
                "plugins": ["wholerow", "checkbox"]
            });
        } else {
            $JuXue.n_danger(r.msg);
        }
    });
}

/**保存用户**/
function saveUserInfo(){
    var ref = $('#deptTree').jstree(true);
    $("[name='deptId']").val(ref.get_selected()[0]);
    var validator = $("#user-add-form").validate();
    var flag = validator.form();
    if (flag) {
        $.post(ctx + "user/add", $("#user-add-form").serialize(), function (r) {
            if (r.code === 0) {
                $("#user_info").modal("hide");
                $JuXue.n_success(r.msg);
                $JuXue.refreshTable("user-data-table");
            } else $JuXue.n_danger(r.msg);
        });
    }else{
        $JuXue.n_danger("请填写完成信息");
    }
}


function init_grade() {
    $.getJSON(ctx+"grade/list",{},function (r) {
        if(r.code===0){
            var data = r.data;
            $.each(data,function (index,val) {
                var id = val.gId;
                var name = val.gName;
                $("#grade").append('<option class="form-control" value="'+id+'">'+name+'</option>')
            })
        }
    })
}